import zhCN from '@/lang/packs/zhCN'
import en from '@/lang/packs/en'

let langObj = zhCN
const formatStr = (str: string, args?: any[]) => {
    if (!str) {
        return str
    }
    let newStr = str
    if (args === undefined) {
        return newStr
    }
    for (let i = 0; i < args.length; i++) {
        newStr = newStr.replace(`{${i}}`, args[i])
    }
    return newStr
}
const setLang = (lang: 'zhCN' | 'en' | any = 'en') => {
    if (lang === 'zhCN') {
        langObj = zhCN
    } else if (lang === 'en') {
        langObj = en
    } else {
        langObj = lang
    }
}
const $t = (tpl: string, args?: any[]) => {
    let fieldList = tpl.split('.')
    let value: any = langObj
    for (let field of fieldList) {
        value = value[field]
    }
    return formatStr(value, args)
}

export {
    setLang, $t
}